﻿using Kingdee.BOS;
using Kingdee.BOS.App;
using Kingdee.BOS.App.Data;
using Kingdee.BOS.Contracts;
using Kingdee.BOS.Core.Report;
using Kingdee.BOS.Util;
using Kingdee.K3.FIN.AR.App.Report;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DzInfo.Report.ND
{
    [Description("应收款明细表服务端插件_纳德扩展")]
    public class ARDetailReportServiceExt : ARDetailReportService
    {
        private String[] customRptTempTableNames;
        public override void BuilderReportSqlAndTempTable(IRptParams filter, string tableName)
        {
            IDBService dBService = ServiceHelper.GetService<IDBService>();
            customRptTempTableNames = dBService.CreateTemporaryTableName(this.Context, 1);
            String strTable = customRptTempTableNames[0];
            base.BuilderReportSqlAndTempTable(filter, strTable);
            StringBuilder sb = new StringBuilder();
            string strSql = @"/*dialect*/ 
            select t1.*, A2.FDATAVALUE 
            into {0}  FROM {1} t1 
            left join t_AR_receivable A1 on t1.FBILLNO = a1.FBILLNO AND T1.FBILLTYPEID='5c6fbeadcb7fe9'
            LEFT JOIN T_BAS_ASSISTANTDATAENTRY_L A2 ON A2.FENTRYID=A1.F_NDN_ASSISTANT1 AND A2.FLOCALEID=2052
            ";
            sb.AppendFormat(strSql, tableName, strTable);
            DBUtils.Execute(this.Context, sb.ToString());
        }
        //public override ReportHeader GetReportHeaders(IRptParams filter)
        //{
        //    ReportHeader header = base.GetReportHeaders(filter);
        //    header.AddChild("FDATAVALUE", new LocaleValue("销售类型"));
        //    return header;
        //}
        public override void CloseReport()
        {
            //删除临时表
            if (customRptTempTableNames.IsNullOrEmptyOrWhiteSpace())
            {
                return;
            }
            IDBService dbService = Kingdee.BOS.App.ServiceHelper.GetService<Kingdee.BOS.Contracts.IDBService>();
            dbService.DeleteTemporaryTableName(this.Context, customRptTempTableNames);
            base.CloseReport();
            //if (temptables != null && temptables.Count() >= 1)
            //{
            //    ITemporaryTableService service = ServiceFactory.GetService<ITemporaryTableService>(base.Context);
            //    service.DropTable(base.Context, new HashSet<string>(temptables));
            //    base.CloseReport();
            //}
        }
    }
}
